/*
 * @Author: lqx 1096345766@qq.com
 * @Date: 2025-04-16 11:38:34
 * @LastEditors: lqx 1096345766@qq.com
 * @LastEditTime: 2025-04-16 13:54:42
 * @FilePath: \jxzr-lims-web\src\store\modules\userInfo.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { authUserInfoApi, menuRoutesApi } from '@/apis/payment'
import { MenuRouteVO, UserInfo } from '@/apis/payment.type'
import { StorageNameEnums } from '@/enums'
import { localBaseRoutes } from '@/router/modules'
import { storageTools } from '@/utils/storage'
import { create } from 'zustand'

interface InitialState {
  userInfo: UserInfo
  permissionList: MenuRouteVO[]
}

interface UserInfoActions {
  getUserInfo: () => Promise<UserInfo | undefined>
  getUserPermissionList: () => Promise<MenuRouteVO[]>
}

export const useUserInfo = create<InitialState & UserInfoActions>((set) => ({
  userInfo: {},
  permissionList: [],
  getUserInfo: async () => {
    const localStorageUserInfo = storageTools.getJSONLocalStorage(StorageNameEnums.UserInfo)
    if (localStorageUserInfo) {
      set({
        userInfo: localStorageUserInfo
      })
      return localStorageUserInfo
    }
    const { success, data } = await authUserInfoApi()
    if (success) {
      set({
        userInfo: data
      })
      return data
    }
  },
  getUserPermissionList: async () => {
    // const { success, data } = await menuRoutesApi()
    // if (success) {
    //   set({
    //     permissionList: data
    //   })
    //   return data
    // }
    set({
      permissionList: localBaseRoutes
    })

    return []
  }
}))
